package com.pro.util.generator;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class MyBatisGenerator {
	static String driver = "com.mysql.jdbc.Driver";  
	static String url = "jdbc:mysql://127.0.0.1:3306/wisdom_fire_system?useUnicode=true&characterEncoding=UTF-8&createDatabaseIfNotExist=true";  
	static String user = "root";  
	static String password = "root"; 
	static String template = "<if test=\"id != null and id !=''\"> and id =#{id} </if> ";
	static String setTemplate = "<if test=\"${columeName} != null and ${columeName} != ''\">${columeName}=#{${columeName},jdbcType=${columeType}}";
	static String daoPath = "";
	static String beanPath = "";
	static String mapPath = "";
	
	public static void main(String[] args) {
		try {
			Class.forName(driver);
			Connection con = DriverManager.getConnection(url, user, password);
//			Statement statement = con.createStatement();  
			List<Table> tableList = dealTable(con);
			GenerateUtil.start(tableList);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println(e);
		}
		
	}
	static String convertType(String type){
		String jdbcType = "VARCHAR";
		switch (type.toUpperCase()) {
		case "INT":
			jdbcType = "INTEGER";
			break;
		default:
			jdbcType = type.toUpperCase();
			break;
		}
		return jdbcType;
	}
	public static String convertDatabaseCharsetType(String in, String type) {  
        String dbUser;  
        if (in != null) {  
            if (type.equals("oracle")) {  
                dbUser = in.toUpperCase();  
            } else if (type.equals("postgresql")) {  
                dbUser = "public";  
            } else if (type.equals("mysql")) {  
                dbUser = null;  
            } else if (type.equals("mssqlserver")) {  
                dbUser = null;  
            } else if (type.equals("db2")) {  
                dbUser = in.toUpperCase();  
            } else {  
                dbUser = in;  
            }  
        } else {  
            dbUser = "public";  
        }  
        return dbUser;  
    }  
	static List<Table> dealTable (Connection connect)throws Exception{
		List<Table> tableList = new ArrayList<Table>();
		DatabaseMetaData dbMetData = connect.getMetaData();  
        // mysql convertDatabaseCharsetType null  
        ResultSet rs = dbMetData.getTables(null,  
                convertDatabaseCharsetType("root", "mysql"), null,  
                new String[] { "TABLE", "VIEW" });  
  
        while (rs.next()) {  
            if (rs.getString(4) != null  
                    && (rs.getString(4).equalsIgnoreCase("TABLE") || rs  
                            .getString(4).equalsIgnoreCase("VIEW"))) {  
                String tableName = rs.getString(3).toLowerCase();
                Table  table = new Table();
                table.setTableName(tableName);
                tableList.add(table);
                // 根据表名提前表里面信息：  
                ResultSet colRet = dbMetData.getColumns(null, "%", tableName,  
                        "%");  
                List<ColumnBean> columeList = new ArrayList<ColumnBean>();
                table.setColumnList(columeList);
                while (colRet.next()) {  
                	ColumnBean columm = new ColumnBean();
                    String columnName = colRet.getString("COLUMN_NAME");  
                    String columnType = colRet.getString("TYPE_NAME");  
                    columm.setName(columnName);
                    columm.setType(convertType(columnType));
                    columeList.add(columm);
                }  
  
            }  
        }
        return tableList;
	}

}
